package org.smoozik.services.musictheory;

import org.smoozik.app.common.partition.component.UIPartition;
import org.smoozik.app.ihm.Constants;

public class NoteGenerator {

	public static Note getRandomNote(UIPartition partition, int gereDieseBemol) {
		int min;
		int max;
		if (partition == null) {
			return null;
		}

		min = partition.getMinimalHeight();
		max = partition.getMaximalHeight();
		int index = getRandomFromBounds(min, max);

		Note note = new Note(partition, index);

		if (gereDieseBemol == 1) {
			int bemol = getRandomFromBounds(-1, 1);
			note.setHauteur(note.getHauteur() + bemol);
		}

		// note.prepareDisplay();

		return note;
	}

	public static Chord getRandomChord(int min, int max, int gereDieseBemol,
			int renversement) {
		// generates the fundamental of the chord
		Note note = NoteGenerator.getRandomNote(min, max, gereDieseBemol);
		Chord chord = new Chord(note);

		// generates the type of the chord
		int alt = NoteGenerator.getRandomFromBounds(0,
				Constants.CHORDS.length - 1);
		chord.setIntervals(Constants.CHORDS[alt]);

		// revert the chord
		if (renversement == 1) {
			int nb = NoteGenerator.getRandomFromBounds(0,
					chord.getNbNotes() - 1);
			chord.renversement(nb);
		}

		return chord;
	}

	public static Chord getRandomIntervalle(int min, int max, int gereDieseBemol) {
		// generates the fundamental of the chord
		Note note = NoteGenerator.getRandomNote(min, max, gereDieseBemol);
		Chord chord = new Chord(note);

		// generates the type of the chord
		int alt = NoteGenerator.getRandomFromBounds(0,
				Constants.INTERVALS.length - 1);
		Interval[] intervals = { Constants.INTERVALS[alt] };
		chord.setIntervals(intervals);

		return chord;
	}

	public static Note getRandomNote(int min, int max, int gereDieseBemol) {
		int index = getRandomFromBounds(min, max);

		Note note = new Note(index);

		if (gereDieseBemol == 1) {
			int bemol = getRandomFromBounds(-1, 1);
			note.setHauteur(note.getHauteur() + bemol);
		}

		return note;
	}

	public static Interval getRandomIntervalleByBounds(int min, int max) {
		int degre = getRandomFromBounds(min, max);

		Interval interval = new Interval(degre);

		return interval;
	}

	public static Interval getRandomIntervalle() {
		return getRandomIntervalleByBounds(1, Constants.INTERVALS.length - 1);
	}

	public static int getRandomFromBounds(int min, int max) {
		int result = 0;
		double rand = Math.random();
		int total = max - min + 1;
		double randExtended = rand * total;
		randExtended -= 0.5;

		result = (int) Math.round(randExtended);
		result += min;

		return result;
	}

	public static void main(String args[]) {
		double[] occurrences = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0 };
		int n = 0;
		int loopCpt = 10000000;
		int total = 0;
		double totalPourcentage = 0;
		double pourcentage = 0;

		for (int i = 0; i < loopCpt; i++) {
			n = NoteGenerator.getRandomFromBounds(0, occurrences.length - 1);

			occurrences[n]++;
		}

		for (int i = 0; i < occurrences.length; i++) {
			pourcentage = (100 * (occurrences[i])) / loopCpt;
			System.out.println(i + " : " + occurrences[i] + " = " + pourcentage
					+ "%");
			total += occurrences[i];
			totalPourcentage += pourcentage;
		}
		System.out.println("total : " + total + " = " + totalPourcentage + "%");

	}

}
